﻿using CIS.Common.Dtos.ClientDtos;
using CIS.Common.Entitys;
using CIS.Server.Common.Responses;
using CIS.Server.Manager.Equipment;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Net;

namespace CIS.Server.Controllers
{
    [Route("[controller]/[action]")]
    [ApiController]
    public class UnitController : ControllerBase
    {
        private readonly IEquipmentManager _equipmentManager;

        public UnitController(IEquipmentManager equipmentManager)
        {
            _equipmentManager = equipmentManager;
        }

        [HttpGet]
        public async Task<ResponseResult> GetUnits()
        {
            ResponseResult response = new ResponseResult();
            try
            {
                List<UnitEntity> units = await _equipmentManager.GetUnitByInLineToolIdAsync("3AFS12");
                if (units != null && units.Count > 0)
                {
                    units = units.OrderBy(u => u.UnitPathNo).ToList();
                    List<UnitClientDto> returnUnits = new List<UnitClientDto>();
                    foreach(var item in units)
                    {
                        UnitClientDto unit = new UnitClientDto();
                        unit.QualifiedLocationId = item.QualifiedLocationId;
                        unit.UnitPathNo = item.UnitPathNo;
                        unit.InlineToolId= item.InlineToolId;
                        unit.SoftwareVersion= item.SoftwareVersion;
                        unit.Category=item.Category;
                        unit.Status= item.Status;
                        unit.CurrentPPId= item.CurrentPPId;
                        unit.CIMMode= item.CIMMode;
                        unit.CurrentRecipeId= item.CurrentRecipeId;
                        returnUnits.Add(unit);
                    }
                    response.IsSuccess = true;
                    response.StatusCode = HttpStatusCode.OK;
                    response.Message = "Operation Is Success";
                    response.Data = returnUnits;
                }
                else
                {
                    response.IsSuccess = false;
                    response.StatusCode = HttpStatusCode.OK;
                    response.Message = "Operation Is Failed Because Can't Find Any Port";
                }
                return response;
            }
            catch (Exception ex)
            {
                response.IsSuccess = false;
                response.StatusCode = HttpStatusCode.InternalServerError;
                response.Message = "Operation Is Failed Because Server Is Error";
                return response;
            }
        }

        [HttpGet]
        public async Task<ResponseResult> GetInlineTool()
        {
            ResponseResult response = new ResponseResult();
            try
            {
                List<InlineToolEntity> inlineTools = await _equipmentManager.GetInlineToolsAsync();
                if (inlineTools != null && inlineTools.Count > 0)
                {
                    InlineToolEntity inlineTool = inlineTools.Single();
                    InlineToolClientDto dto = new InlineToolClientDto();
                    dto.InlineToolId = inlineTool.InlineToolId;
                    dto.InlineType = inlineTool.InlineType;
                    dto.EquipmentMode = inlineTool.EquipmentMode;
                    dto.OperationName = inlineTool.OperationName;
                    dto.ControlState = inlineTool.ControlState;
                    dto.ShopName = inlineTool.ShopName;
                    dto.SoftwareVersion = inlineTool.SoftwareVersion;
                    response.IsSuccess = true;
                    response.StatusCode = HttpStatusCode.OK;
                    response.Message = "Operation Is Success";
                    response.Data = dto;
                }
                else
                {
                    response.IsSuccess = false;
                    response.StatusCode = HttpStatusCode.OK;
                    response.Message = "Operation Is Failed Because Can't Find Any Port";
                }
                return response;
            }
            catch (Exception ex)
            {
                response.IsSuccess = false;
                response.StatusCode = HttpStatusCode.InternalServerError;
                response.Message = "Operation Is Failed Because Server Is Error";
                return response;
            }
        }
    }
}
